﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using PPI.Client.Logic.Inspection;
using PPI.Client.Logic.XML;
using PPI.Client.Utils;
using PPI.Entities.Inspections;
using PPI.Entities.XML;

namespace PPI.Client.Logic.Upload
{
    public class LUploadStrategyInspection : LUploadStrategyBase
    {
        private bool m_hasErrors = false;

        public LUploadStrategyInspection(EXmlOutboxBase outbox)
        :base(outbox)
        {
        }

        public override void StartWork(BackgroundWorker worker, DoWorkEventArgs workerEvents)
        {
            List<EInspectionResult> _ListResults =
                LInspectionResult.GetMobileInspectionResults(m_OutboxDatabase.ConnectionString);

            Int32 _TotalToUpload = _ListResults.Count*2;
            Int32 _CountUploaded = _TotalToUpload;

            foreach (EInspectionResult result in _ListResults)
            {
                if (worker.CancellationPending)
                {
                    workerEvents.Cancel = true;
                    return;
                }
                if (result.Inspection.ServerID == Int32.MinValue)
                {
                    try
                    {
                        result.Inspection.ServerID = LInspection.Save(result.Inspection);
                    }
                    catch(Exception ex)
                    {
                        RegisterError.SaveError(ex);
                        m_HasErrors = true;
                    }
                    finally
                    {
                        LInspection.UpdateUploadedData(result.Inspection, m_OutboxDatabase.ConnectionString);
                        _CountUploaded--;
                        worker.ReportProgress(ReportProgress(_CountUploaded, _TotalToUpload));
                    }
                    System.Threading.Thread.Sleep(100);
                }

                if (result.SeverId == Int32.MinValue)
                {
                    try
                    {
                        result.SeverId = LInspectionResult.Save(result);
                    }
                    catch(Exception ex)
                    {
                        RegisterError.SaveError(ex);
                        m_HasErrors = true;
                    }
                    finally
                    {
                        LInspectionResult.UpdateUploadedData(result, m_OutboxDatabase.ConnectionString);
                        _CountUploaded--;
                        worker.ReportProgress(ReportProgress(_CountUploaded, _TotalToUpload));
                    }
                    System.Threading.Thread.Sleep(100);
                }

                if (result.SeverId > 0 && result.Inspection.ServerID > 0 && (result.IsElectrical || EResultType.ParseValue(result.Resultype.Name) >= 2))
                {
                    try
                    {
                        LInspection.CreateRemedy(result.Inspection.ServerID, result.Inspection.AcquisitionDate, result.Resultype.Id);
                    }
                    catch(Exception ex)
                    {
                        RegisterError.SaveError(ex);
                        m_HasErrors = true;
                    }
                }

                if (worker.CancellationPending)
                {
                    workerEvents.Cancel = true;
                    return;
                }
            }
            worker.ReportProgress(100);
        }

        public override void FinishWork()
        {
            if(!m_HasErrors)
            {
                LXmlOutbox _serviceLogic = new LXmlOutbox(m_OutboxDatabase);
                m_OutboxDatabase.IsDataUploaded = true;
                m_OutboxDatabase.DataFinishedToUpload = DateTime.Now;
                _serviceLogic.UpdateNode(m_OutboxDatabase);
            }

        }
    }
}
